# -*- coding: utf-8 -*-
"""
轮动策略框架 | 邢不行 | 2024分享会
author: 邢不行
微信: xbx6660
"""
import pandas as pd
from program.Config import *

# 动态读取config里面配置的shift_name脚本
Shift = __import__('program.shift.%s' % shift_name, fromlist=('',))

# =====取出RShift中配置的变量
# 从Shift中取出 回测持仓周期hold_period
hold_period = Shift.hold_period
# 从Shift中取出 offset
offset = Shift.offset
# 从Shift中取出 策略名stg_name
stg_name = Shift.stg_name

rtn_path = os.path.join(back_test_path, f'{stg_name}_策略评价_{hold_period}.csv')
select_stat_path = os.path.join(back_test_path, f'统计信息/{stg_name}_子策略表现_{hold_period}_{offset}.csv')
times_path = os.path.join(back_test_path, f'统计信息/{stg_name}_选中次数_{hold_period}_{offset}.csv')

try:
    rtn = pd.read_csv(rtn_path, encoding='gbk', index_col=[0])
    select_stat = pd.read_csv(select_stat_path, encoding='gbk')
    times = pd.read_csv(times_path, encoding='gbk')
except:
    print('='*50)
    print('请检查一下是否运行了 2_计算多offset资金曲线.py ')
    print('='*50)
    import traceback
    traceback.print_exc()
    exit()

# 低版本的pandas，不支持 index 参数
# 安装高版本 pandas 命令 ： pip install pandas==1.5.3
tx_rtn = rtn.to_markdown()

tx_select_stat = select_stat.to_markdown(index=False)
tx_times = times.to_markdown(index=False)

with open(root_path + '/program/发帖脚本/样本模板.txt', 'r', encoding='utf8') as file:
    bbs_post = file.read()
    bbs_post = bbs_post % (tx_rtn, tx_select_stat, tx_times)
    print(bbs_post)
